package com.infore.statisticsAndAlarm.service.impl;

import java.time.LocalDate;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.infore.statisticsAndAlarm.constant.Constants;
import com.infore.statisticsAndAlarm.dto.WaterInstructionsDataDto;
import com.infore.statisticsAndAlarm.entity.MonthData;
import com.infore.statisticsAndAlarm.enums.ResultStatus;
import com.infore.statisticsAndAlarm.repository.IMonthDataRepository;
import com.infore.statisticsAndAlarm.service.IAnalysisWeChatService;
import com.infore.statisticsAndAlarm.util.ArrayUtils;
import com.infore.statisticsAndAlarm.util.DateUtil;
import com.infore.statisticsAndAlarm.util.ReflexUtil;
import com.infore.statisticsAndAlarm.util.ResultEntity;
import com.infore.statisticsAndAlarm.util.WaterQualityUtil;
import com.infore.statisticsAndAlarm.vo.ChartsVo;

@Service
public class AnalysisWeChatServiceImpl implements IAnalysisWeChatService{

	@Autowired
	private IMonthDataRepository iMonthDataRepository;
	
	public ResultEntity waterQuality(String time, String mn) {
		//数据查询
		LocalDate localDate = LocalDate.parse(time+"-01-01");
		Date year = Date.from(localDate.atStartOfDay(ZoneId.systemDefault()).toInstant());
		Date start = DateUtil.getFirstDay_Year(year);
		Date end = DateUtil.getFinalyDay_Year(year);
		List<MonthData> monthDatas = iMonthDataRepository
				.findByStationCodeInAndDataTimeBetweenOrderByDataTimeAsc(ArrayUtils.stringToList(mn), start, end);
		Map<String, List<MonthData>> glist = monthDatas.stream()
				.collect(Collectors.groupingBy(g->DateUtil.dateToString(g.getDataTime(), DateUtil.DATE_SMALL_YM)));
		List<String> timeList = DateUtil
				.getTimeList(DateUtil.dateToString(start, DateUtil.DATE_SMALL_YM),
						DateUtil.dateToString(end, DateUtil.DATE_SMALL_YM), DateUtil.DATE_SMALL_YM, "month");
		// 初始化echarts实体
		ChartsVo chartsVo = new ChartsVo();
		List<String> xAxis = new ArrayList<String>(); // X轴数据集
		List<Map<String, Object>> series = new ArrayList<Map<String, Object>>(); // 系列列表数据集
		List<String> level = new ArrayList<String>();
		List<String> compositeIndexs = new ArrayList<String>();
		List<WaterInstructionsDataDto> InstructionsList = null;
		for (String month : timeList) {
			List<MonthData> glistMonth = glist.get(month);
			month = month.substring(5)+"月";
			if (month.startsWith("0")) {
				month = month.replace("0", "");
			}
			xAxis.add(month);
			if (glistMonth == null || glistMonth.size() == 0) {
				level.add(null);
				compositeIndexs.add(null);
				continue;
			}//计算水质等级和综合指数
			InstructionsList = new ArrayList<WaterInstructionsDataDto>();
			WaterInstructionsDataDto Instructions = new WaterInstructionsDataDto();// 计算水质等级封装类
			for (MonthData monthData : glistMonth) {
				ReflexUtil.setValue(Instructions, WaterInstructionsDataDto.class, 
						monthData.getFactorCode(), String.class, monthData.getDataValueAvg());
				InstructionsList.add(Instructions);
			}
			WaterQualityUtil.setWaterQuality(InstructionsList);
			String compositeIndex = WaterQualityUtil.countCompositeIndex(Instructions);
			level.add(Instructions.getWaterQuality());
			compositeIndexs.add(compositeIndex);
		}
		Map<String, Object> levelMap = new HashMap<String, Object>();
		levelMap.put("name", "水质类别");
		levelMap.put("value", level);
		Map<String, Object> compositeIndexsMap = new HashMap<String, Object>();
		compositeIndexsMap.put("name", "水污染指数");
		compositeIndexsMap.put("value", compositeIndexs);
		series.add(levelMap);
		series.add(compositeIndexsMap);
		chartsVo.setxAxis(xAxis);
		chartsVo.setSeries(series);
		return new ResultEntity(ResultStatus.SUCCESS,Constants.QUERY_SUCCESS).setInfo(chartsVo);
	}


}
